package com.xk.supply.utils;

import cn.hutool.core.util.StrUtil;
import com.xk.supply.pojo.dto.OpenSynParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Collection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
@Component
public class CommonUtils {

    public static void validateParam(Object object, String... fields) throws Exception {
        Assert.notNull(object, "param is null!");
        if (fields != null && fields.length != 0) {
            Class c = object.getClass();
            String[] var3 = fields;
            int var4 = fields.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String field = var3[var5];
                String methodName = "get".concat(field.substring(0, 1).toUpperCase()).concat(field.substring(1, field.length()));

                Method method;
                try {
                    method = c.getDeclaredMethod(methodName, (Class[])null);
                } catch (NoSuchMethodException var10) {
                    method = c.getSuperclass().getDeclaredMethod(methodName, (Class[])null);
                }

                Object obj = method.invoke(object);
                if (obj == null) {
                    throw new IllegalArgumentException(field + " is null!");
                }

                if (obj instanceof String && StrUtil.isBlank(obj.toString())) {
                    throw new IllegalArgumentException(field + " is null!");
                }

                if (obj instanceof Collection && ((Collection)obj).size() == 0) {
                    throw new IllegalArgumentException(field + " is null!");
                }
            }

        }
    }

    public static String getIpAddr(HttpServletRequest request) {
        //request 可以自己做ip白名单
        String ip = request.getHeader("x-forwarded-for");
        log.info("validateParam x-forwarded-for ip : {}",ip);
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
            log.info("validateParam Proxy-Client-IP ip : {}",ip);
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
            log.info("validateParam WL-Proxy-Client-IP ip : {}",ip);
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
            log.info("validateParam unknown : {}",ip);
        }
    return ip;
    }

    public void verifySign(OpenSynParam openSynParam) {
        //获取秘钥，暂时先写死
        String secret = "";
        String appKey = "";

        StringBuilder sb = new StringBuilder();
        sb.append("appKey").append(appKey);
        sb.append("format").append(openSynParam.getFormat());
        sb.append("paramJson").append(openSynParam.getParamJson());
        sb.append("timestamp").append(openSynParam.getTimestamp());
        sb.append("v").append(openSynParam.getV());
        sb.append("secret").append(secret);
        String beforeMD5 = sb.toString();
        String md5String = MD5Util.md5(beforeMD5).toUpperCase();
        if (!md5String.equals(openSynParam.getSign())) {
            log.info("openApi sign verify failed. beforeMd5 = {}, md5String = {}, openSynParamSign = {}", beforeMD5, md5String, openSynParam.getSign());
            throw new RuntimeException("验签失败");
        }
    }

    public static void main(String[] args) {

    }
}
